package cn.chenhuanming.shh.administer.intermediary.business.controller;

import cn.chenhuanming.shh.administer.intermediary.business.service.HouseService;
import cn.chenhuanming.shh.administer.repository.HouseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by chenhuanming on 2017-07-27.
 *
 * @author chenhuanming
 */
@RepositoryRestController
@RequestMapping("/houses")
public class HouseController {

    @Autowired
    HouseService houseService;

    @Autowired
    HouseRepository houseRepository;

    @Autowired
    RepositoryEntityLinks entityLinks;

    @Autowired
    PagedResourcesAssembler assembler;

    /**
     *
     * @param type "出租房" "二手房"
     * @param name  "房源名"
     * @param cityName  "城市名"
     * @param cityAreaName  "城区名"
     * @param lowArea   "最小面积"
     * @param highArea  "最大面积"
     * @param lowPrice  "最低价格" (Float)
     * @param highPrice "最高价格"  (Float)
     * @param isQuality "精品房源"  "是"
     * @param isAnxious "急售房源"  "是"
     * @param isRecommended "推荐房源" "是"
     * @param pageable  ""分页
     * @param sortType desc asc
     * @param sortBy createTime area price
     * @param
     */
    @RequestMapping(value = "/search/findHouseDynamic",method = RequestMethod.GET)
    public ResponseEntity<?> findByType
    (String type, String name, String cityName, String cityAreaName,
      Float lowArea,  Float highArea,
      Float lowPrice, Float highPrice,
     String isQuality, String isAnxious, String isRecommended,
     String sortType, String sortBy,
     Pageable pageable,PersistentEntityResourceAssembler resourceAssembler) {
        return new ResponseEntity<>(assembler.toResource(houseRepository.findAll(
                houseService.findHouseBySpecification(type,
                        name, cityName, cityAreaName,
                        lowArea, highArea,
                        lowPrice, highPrice,
                        isQuality, isAnxious, isRecommended, sortType, sortBy), pageable),resourceAssembler), HttpStatus.OK);
//        List<House> houses =  houseRepository.findAll(houseService.findSSS(isQuality, lowArea));
//        return ResponseEntity(assembler.toResource(), HttpStatus.OK);
//        return;
    }

//    /**
//     * 发布房源，仅经纪人可以发布，并且会进行相关检查
//     * @param house
//     * @param principal
//     * @return ResponseEntity
//     */
//    @PreAuthorize("hasRole('ROLE_SHOP_BROKER')")
//    @PostMapping
//    public ResponseEntity createHouses(@RequestBody House house, Principal principal){
//        House creation = houseService.create(house,principal.getName());
//
//        //写入resource（rest hal）
//        Resource<House> resource = new Resource<>(creation);
//        resource.add(entityLinks.linkToSingleResource(creation));
//        return ResponseEntity.ok(resource);
//    }
//    @GetMapping("/house/test")
//    public ResponseEntity findByName(@RequestParam String name,
//                                     @RequestParam(required = false) Integer equityYear) {
//        House house = new House();
//        house.setName(name);
//        house.setEquityYear(equityYear);
//
//        Resources<House> houses = new Resources(houseService.findHouse(house));
//
//        houses.add(linkTo(methodOn(HouseController.class).findByName("",null)).withRel("self"));
//
//        return ResponseEntity.ok(houses);
//    }
}
